package com.kingint.web.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.entity.UmsPermission;
import com.kingint.mbg.sys.entity.UmsRole;
import com.kingint.mbg.sys.entity.UmsRolePermissionRelation;
import com.kingint.mbg.sys.mapper.UmsRoleMapper;
import com.kingint.mbg.sys.mapper.UmsRolePermissionRelationMapper;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.sys.service.UmsRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-09-18
 */
@Service
@Slf4j
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements UmsRoleService {

    @Resource
    private UmsRoleMapper roleMapper;
    @Resource
    private UmsRolePermissionRelationMapper rolePermissionRelationMapper;
    @Resource
    private UmsAdminService adminService;


    @Override
    public Integer insertRoleInfo(UmsRole role) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        role.setHotelId(hotelId);
        role.setCreateTime(new Date());

        int insert = roleMapper.insert(role);
        //同时处理权限问题
        handlePermission(role.getId(),role.getMenuIdList());

        return insert;
    }

    @Override
    public IPage<UmsRole> getRoleList(UmsRole role, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店id");
            return null;
        }
        IPage<UmsRole> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId)
                .eq("status",0);
        String name = role.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        IPage<UmsRole> umsRoleIPage = roleMapper.selectPage(page, wrapper);
        return umsRoleIPage;
    }

    @Override
    public Integer deleteRoleInfo(Long id) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        umsRole.setStatus(1);
        int i = roleMapper.updateById(umsRole);
        return i;
    }

    @Override
    public List<UmsPermission> getPermissionList(Long roleId) {
        List<UmsPermission> permissionList = rolePermissionRelationMapper.getPermissionList(roleId);
        return permissionList;
    }

    @Override
    public List<UmsRolePermissionRelation> getRolePermission(Long roleId) {
        QueryWrapper<UmsRolePermissionRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<UmsRolePermissionRelation> umsRolePermissionRelations = rolePermissionRelationMapper.selectList(queryWrapper);
        return umsRolePermissionRelations;
    }

    @Override
    public Integer updateRoleInfo(UmsRole role) {
        Set<Long> idList = role.getMenuIdList();
        handlePermission(role.getId(),idList);
        int i = roleMapper.updateById(role);
        return i;
    }

    @Override
    public List<UmsRole> getAllRoleList() {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店id");
            return null;
        }
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId)
                .eq("status",0);
        List<UmsRole> umsRoles = roleMapper.selectList(wrapper);
        return umsRoles;
    }

    /**
     * 处理相关权限
     * @param id
     * @param idList
     */
    private void handlePermission(Long id, Set<Long> idList) {
    //先删除原有关系
        QueryWrapper<UmsRolePermissionRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",id);
        rolePermissionRelationMapper.delete(wrapper);

        //批量插入新关系
        List<UmsRolePermissionRelation> relationList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(idList)){
            for (Long permissionId : idList) {
                UmsRolePermissionRelation relation = new UmsRolePermissionRelation();
                relation.setRoleId(id);
                relation.setPermissionId(permissionId);
                relationList.add(relation);
            }
            rolePermissionRelationMapper.insertList(relationList);
        }
    }
}
